
# - 通过ip请求淘宝的一个接口获取到省份和运营商
# - 接口：https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=要查询的ip地址
# - 每一行变成这种形式保存到文件
# - 格式：ip,中国,湖南,电信


import requests
import json

#获取ip的信息
ip=input("请输入ip地址：")
def get_ip_info(ip):
    url = f"https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ip}"
    response=requests.get(url)
    data=json.loads(response.text)
    queryIp,country,region,city,isp=(
        data['data']['queryIp'],
        data['data']['country'],
        data['data']['region'],
        data['data']['city'],
        data['data']['isp'],
    )
    return queryIp,country,region,city,isp
queryIp,country,region,city,isp=get_ip_info(ip)

data_ip=open("ip_info.txt","w",encoding="utf-8")
data_ip.writelines(queryIp+",")
data_ip.writelines(country+",")
data_ip.writelines(region+",")
data_ip.writelines(city+",")
data_ip.writelines(isp)